package com.zzedu.common.utils;

import com.zzedu.common.core.domain.entity.SysUser;
import com.zzedu.common.exception.ServiceException;
import com.zzedu.common.utils.security.LoginUser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * shiro 工具类
 *
 * @author smarthome
 */
public class SecurityUtils {

    public static void logout() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        //清除认证
        if (auth != null) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
    }

    public static SysUser getSysUser() {
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        Object object = auth.getPrincipal();
        if (object instanceof LoginUser) {
            LoginUser loginUser = (LoginUser) object;
            return loginUser.getUser();
        }
        return null;
    }

    public static void setSysUser(SysUser user) {

    }

    public static Long getUserId() {
        return getSysUser().getId().longValue();
    }

    public static String getLoginName() {
        return getSysUser().getUserName();
    }

    public static String getIp() {
        return IpUtils.getIpAddr(ServletUtils.getRequest());
    }

    public static String getSessionId() {
        return ServletUtils.getRequest().getSession(true).getId();
    }


    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 密码
     * @return 加密字符串
     */
    public static String encryptPassword(String password) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2Y);
        return passwordEncoder.encode(password);
    }

    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser() {
        try {
            return (LoginUser) getAuthentication().getPrincipal();
        } catch (Exception e) {
            throw new ServiceException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
        }
    }
    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication()
    {
        return SecurityContextHolder.getContext().getAuthentication();
    }


    /**
     * 判断密码是否相同
     *
     * @param rawPassword     真实密码
     * @param encodedPassword 加密后字符
     * @return 结果
     */
    public static boolean matchesPassword(String rawPassword, String encodedPassword) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2Y);
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }
    /**
     * 是否为管理员
     *
     * @param userId 用户ID
     * @return 结果
     */
    public static boolean isAdmin(Long userId)
    {
        return userId != null && 1L == userId;
    }
}
